package com.hjm.project.merchantModelDetail.service.impl;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.common.utils.text.Convert;
import com.hjm.project.merchantModelDetail.domain.MerchantModelDetail;
import com.hjm.project.merchantModelDetail.domain.MerchantModelDetailVo;
import com.hjm.project.merchantModelDetail.mapper.MerchantModelDetailMapper;
import com.hjm.project.merchantModelDetail.service.IMerchantModelDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 费率模版详情Service业务层处理
 * 
 * @author surplus
 * @date 2022-07-10
 */
@Service
public class MerchantModelDetailServiceImpl implements IMerchantModelDetailService {
    @Resource
    private MerchantModelDetailMapper merchantModelDetailMapper;

    /**
     * 查询费率模版详情
     * 
     * @param id 费率模版详情ID
     * @return 费率模版详情
     */
    @Override
    public MerchantModelDetail selectMerchantModelDetailById(Integer id) {
        return merchantModelDetailMapper.selectMerchantModelDetailById(id);
    }

    /**
     * 查询费率模版详情列表
     * 
     * @param merchantModelDetail 费率模版详情
     * @return 费率模版详情
     */
    @Override
    public List<MerchantModelDetail> selectMerchantModelDetailList(MerchantModelDetail merchantModelDetail) {
        return merchantModelDetailMapper.selectMerchantModelDetailList(merchantModelDetail);
    }

    /**
     * 查询费率模版详情列表
     *
     * @param merchantModelDetail 费率模版详情
     * @return 费率模版详情 一个结果
     */
    @Override
    public MerchantModelDetail selectMerchantModelDetailOne(MerchantModelDetail merchantModelDetail) {
        return merchantModelDetailMapper.selectMerchantModelDetailOne(merchantModelDetail);
    }

    /**
     * 新增费率模版详情
     * 
     * @param merchantModelDetail 费率模版详情
     * @return 结果
     */
    @Override
    public int insertMerchantModelDetail(MerchantModelDetail merchantModelDetail) {
        return merchantModelDetailMapper.insertMerchantModelDetail(merchantModelDetail);
    }

    /**
     * 修改费率模版详情
     * 
     * @param merchantModelDetail 费率模版详情
     * @return 结果
     */
    @Override
    public int updateMerchantModelDetail(MerchantModelDetail merchantModelDetail) {
        return merchantModelDetailMapper.updateMerchantModelDetail(merchantModelDetail);
    }

    /**
     * 删除费率模版详情对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMerchantModelDetailByIds(String ids) {
        return merchantModelDetailMapper.deleteMerchantModelDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除费率模版详情信息
     * 
     * @param id 费率模版详情ID
     * @return 结果
     */
    @Override
    public int deleteMerchantModelDetailById(Integer id) {
        return merchantModelDetailMapper.deleteMerchantModelDetailById(id);
    }

    /**
     * 新增/编辑商户模版费率
     * @Param [id]
     * @Return int
     */
    @Override
    @Transactional
    public void saveModelDetailsRate(MerchantModelDetailVo modelDetailVo, List<MerchantModelDetail> modelDetailList) throws Exception {
        try {
            for (MerchantModelDetail modelDetail : modelDetailList) {
                modelDetail.setModelNo(modelDetailVo.getModelNo());
            }
            merchantModelDetailMapper.deleteMerchantModelRateByNo(modelDetailVo.getModelNo());
            merchantModelDetailMapper.insertMerchantModelRateBatch(modelDetailList);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 附加费率详情
     *
     * @param fjCode 附加费率代号
     * @Param [modelDetailVo, modelDetailList]
     * @Return void
     */
    @Override
    public void saveAddFj(String fjCode,String modelNo) throws Exception {
        //获取银行卡费率代号信息
        //银行卡
        MerchantModelDetail mmd = new MerchantModelDetail();
        mmd.setRateType(0);
        //结算类型 0-D0、1-T1
        mmd.setSettleType(0);
        mmd.setCardType(2);
        mmd.setRateAffix(fjCode);
        mmd.setAddMan(ShiroUtils.getLoginName());
        mmd.setAddDate(DateUtils.getDate());
        mmd.setAddTime(DateUtils.getOnlyTime());
        mmd.setDearFj(fjCode);
        mmd.setModelNo(modelNo);
        merchantModelDetailMapper.deleteMerchantModelRateByNo(modelNo);
        merchantModelDetailMapper.insertMerchantModelDetail(mmd);

    }
}
